package org.dev.vlp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dev.vlp.constants.LanguageEnum;
import org.dev.vlp.entity.Book;
import org.dev.vlp.entity.Statistics;
import org.dev.vlp.exception.ApiException;
import org.dev.vlp.mapper.StatisticsMapper;
import org.dev.vlp.service.BookService;
import org.dev.vlp.mapper.BookMapper;
import org.dev.vlp.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Queue;

/**
* @author 86184
* @description 针对表【vlp_vi_book】的数据库操作Service实现
* @createDate 2023-01-15 21:33:39
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService {

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public List<Book> getBookList(int userId) {
        int lan = statisticsService.getLan(userId);
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.eq("lan",lan);
//        LanguageEnum languageEnum = LanguageEnum.get(lan);
//        String tableName = languageEnum.getBookTableEnum().getTableName();
        return bookMapper.selectList(bookQueryWrapper);
    }

    @Override
    public Book getBookDetail(long id,int userId) {
        int lan = statisticsService.getLan(userId);
//        LanguageEnum languageEnum = LanguageEnum.get(lan);
//        String tableName = languageEnum.getBookTableEnum().getTableName();
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.eq("lan",lan).eq("id",(int)id);
        return bookMapper.selectOne(bookQueryWrapper);
    }

    @Override
    public Statistics choiceBook(Long uid, int bookId) {
        // 获取用户所学语种对应的表名
        int lan = statisticsService.getLan(uid.intValue());
        LanguageEnum languageEnum = LanguageEnum.get(lan);
        int beginId = languageEnum.getBookIdEnum().getBookId();

        Book book = bookMapper.selectById(bookId);
        if(book==null){
            throw new ApiException("词书不存在");
        }

        Statistics statistics = statisticsMapper.getBook(uid.intValue(),lan,bookId);
        // 搜索0或bookId的书，0表示用户是第一次选择词书，bookId表示之前已选过
        // 2023.10.31现在用户首次设置语言默认选择第一本词书，后面有两种情况词书改变，一是用户自己改词书，二是词汇量测试出结果用户自己改词书
        if(statistics!=null){
//            if(statistics.getBookId()==0){
//                statistics.setBookId(bookId);
//            }
            statistics.setGmtModified(new Date());
            statisticsMapper.updateById(statistics);
            return statistics;
        }else{
            // TODO: 2023/3/26 选择词书时先查询该语种有多少本书，比如5，若超过，返回信息；没超过则插入数据
            QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
            bookQueryWrapper.eq("lan",lan);
            int count = Math.toIntExact(bookMapper.selectCount(bookQueryWrapper));
            if(bookId>beginId+count-1){
                throw new ApiException("该语种只有"+String.valueOf(count)+"本词书");
            }
            Statistics statistics1 = new Statistics(uid.intValue(), lan, bookId);
            if(statisticsMapper.insert(statistics1)<=0){
                throw new ApiException("新增失败");
            }else{
                return statistics1;
            }
        }
    }
}




